"""
Case Type   : gms_xmlgen高级包
Case Name   : 主备环境执行gms_UTILITY.IS_CLUSTER_DATABASE函数
Create At   : 2025/5/22
Owner       : zhanghuan
Description :
    1.创建插件
    2.使用函数无括号
    3.使用函数有括号
    4.清理环境
Expect      :
    1.创建成功
    2.成功
    3.成功
    4.清理环境完成
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

COMMONSH = CommonSH("PrimaryDbUser")


@unittest.skipIf(1 == COMMONSH.get_node_num(), "单机不执行")
class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()

    def test_pscp(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '--step1:创建gms_xmlgen插件;expect:创建成功--'
        self.log.info(text)
        create_cmd = (f"drop extension if exists gms_utility;"
                      f"create extension gms_utility;")
        self.log.info(create_cmd)
        sql_res = COMMONSH.execut_db_sql(create_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE EXTENSION', sql_res, '执行失败' + text)

        text = '--step2:无括号;;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select gms_UTILITY.IS_CLUSTER_DATABASE;"
        self.log.info(sql_cmd)
        sql_res = COMMONSH.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('f', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step3:有括号;;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select gms_UTILITY.IS_CLUSTER_DATABASE();"
        self.log.info(sql_cmd)
        sql_res = COMMONSH.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('f', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境;expect:清理环境成功--'
        self.log.info(text)
        sql_cmd = f"drop extension if exists gms_utility;"
        self.log.info(sql_cmd)
        sql_res = COMMONSH.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP EXTENSION', sql_res, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
